package org.seven.jrdp.commons.plugin.ukey;

import org.seven.jrdp.commons.util.StringUtils;

public abstract class UkeyPlugin {
	private static final SoftKey SOFT_KEY = new SoftKey();
	private static final int KEY_LENGTH = 32;

	public static String encrypt(String inString, String key) {
		return SOFT_KEY.StrEnc(inString, initKey(key));
	}

	public static String decrypt(String inString, String key) {
		return SOFT_KEY.StrDec(inString, initKey(key));
	}

	private static String initKey(String key) {
		key = StringUtils.getString(key);
		int result = KEY_LENGTH - key.length();
		if (result == 0) {
			return key;
		}
		if (result < 0) {
			return key.substring(0, KEY_LENGTH);
		}
		StringBuffer sb = new StringBuffer(key);
		for (int i = 0; i < result; i++) {
			sb.append("0");
		}
		return sb.toString();
	}
}